package com.ce.pms.system.custservice.service.impl;


import java.util.List;

import com.ce.common.util.BlankUtil;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.ICustEventKindDao;
import com.ce.pms.system.custservice.dao.ICustEventNatureDao;
import com.ce.pms.system.custservice.dao.IEmergencyDegreeDao;
import com.ce.pms.system.custservice.dao.IReturnVisitRuleDao;
import com.ce.pms.system.custservice.model.CustEventKindDO;
import com.ce.pms.system.custservice.model.CustEventNatureDO;
import com.ce.pms.system.custservice.model.EmergencyDegreeDO;
import com.ce.pms.system.custservice.model.ReturnVisitRuleDO;
import com.ce.pms.system.custservice.service.IReturnVisitRuleService;
import com.ce.pms.system.custservice.vo.ReturnVisitRuleVO;
import com.sitechasia.webx.core.support.Page;


/**
 * 描述：回访规则service实现
 * @author 王浩羽
 * 
 */
public class ReturnVisitRuleServiceImpl extends AbstractCustomerEventServiceImpl<ReturnVisitRuleVO, ReturnVisitRuleDO>
	implements IReturnVisitRuleService
{
	private IReturnVisitRuleDao returnVisitRuleDao;
	private ICustEventNatureDao custEventNatureDao;
	private ICustEventKindDao custEventKindDao;
	private IEmergencyDegreeDao emergencyDegreeDao;


	public boolean addReturnVisitRule(ReturnVisitRuleVO returnVisitRuleVO)
	{
		if(!BlankUtil.isBlank(returnVisitRuleVO) && !BlankUtil.isBlank(returnVisitRuleVO.getName()))
		{
			if(isNameExisit(returnVisitRuleVO.getName()))
			{
				throw new BusinessException("custservice:system_name_exist_exception", BusinessException.WARNING);
			}
			ReturnVisitRuleDO returnVisitRuleDO = assembleV2D(returnVisitRuleVO, null);

			if(!BlankUtil.isBlank(returnVisitRuleVO.getCustServEventKindId()))
			{
				CustEventKindDO custEventKindDO = custEventKindDao.findById(returnVisitRuleVO.getCustServEventKindId());
				returnVisitRuleDO.setCustServEventKind(custEventKindDO);
				if(!BlankUtil.isBlank(returnVisitRuleVO.getCustServEventNatureId()))
				{
					CustEventNatureDO custEventNatureDO = custEventNatureDao
																			.findById(returnVisitRuleVO
																										.getCustServEventNatureId());
					custEventKindDO.setCustEventNature(custEventNatureDO);
				}
			}
			if(!BlankUtil.isBlank(returnVisitRuleVO.getEmergencyDegreeId()))
			{
				EmergencyDegreeDO emergencyDegreeDO = emergencyDegreeDao
																		.findById(returnVisitRuleVO
																									.getEmergencyDegreeId());
				returnVisitRuleDO.setEmergencyDegree(emergencyDegreeDO);
			}
			returnVisitRuleDO.setSysInfo(createInfo(returnVisitRuleVO.getCreator()));
			return returnVisitRuleDao.saveObject(returnVisitRuleDO) != null;
		}
		return false;
	}


	public boolean deleteReturnVisitRuleById(Long returnVisitRuleId)
	{
		if(returnVisitRuleId == null)
		{
			return false;
		}
		try
		{
			returnVisitRuleDao.deleteObjectById(returnVisitRuleId);
			return true;
		}
		catch(RuntimeException e)
		{ // 如果发生异常，则认为是已经被引用，那么重新抛出异常
			throw new BusinessException("custservice:system_delete_inused_exception", BusinessException.WARNING);
		}
	}


	public boolean editReturnVisitRule(ReturnVisitRuleVO returnVisitRuleVO)
	{
		if(!BlankUtil.isBlank(returnVisitRuleVO) && !BlankUtil.isBlank(returnVisitRuleVO.getKeyword())
			&& !BlankUtil.isBlank(returnVisitRuleVO.getName()))
		{
			ReturnVisitRuleDO returnVisitRuleDO = returnVisitRuleDao.findById(returnVisitRuleVO.getKeyword());
			if(returnVisitRuleDO == null)
			{
				return false;
			}
			returnVisitRuleDO.setRemark(returnVisitRuleVO.getRemark());
			returnVisitRuleDO.setSequence(returnVisitRuleVO.getSequence());
			returnVisitRuleDO.setValidity(returnVisitRuleVO.getValidity());
			returnVisitRuleDO.setName(returnVisitRuleVO.getName());
			if(!BlankUtil.isBlank(returnVisitRuleVO.getCustServEventKindId()))
			{
				CustEventKindDO custEventKindDO = custEventKindDao.findById(returnVisitRuleVO.getCustServEventKindId());
				returnVisitRuleDO.setCustServEventKind(custEventKindDO);
				if(!BlankUtil.isBlank(returnVisitRuleVO.getCustServEventNatureId()))
				{
					CustEventNatureDO custEventNatureDO = custEventNatureDao
																			.findById(returnVisitRuleVO
																										.getCustServEventNatureId());
					custEventKindDO.setCustEventNature(custEventNatureDO);
				}
			}
			if(!BlankUtil.isBlank(returnVisitRuleVO.getEmergencyDegreeId()))
			{
				EmergencyDegreeDO emergencyDegreeDO = emergencyDegreeDao
																		.findById(returnVisitRuleVO
																									.getEmergencyDegreeId());
				returnVisitRuleDO.setEmergencyDegree(emergencyDegreeDO);
			}
			modifyInfo(returnVisitRuleDO.getSysInfo(), returnVisitRuleVO.getModifier());
			return returnVisitRuleDao.updateObject(returnVisitRuleDO) != null;
		}
		return false;
	}


	@Override
	protected ReturnVisitRuleVO assembleD2V(ReturnVisitRuleDO sourceDO, ReturnVisitRuleVO targetVO)
	{
		ReturnVisitRuleVO resultVO = super.assembleD2V(sourceDO, targetVO);
		if(!BlankUtil.isBlank(sourceDO.getCustServEventKind()))
		{
			CustEventKindDO custEventKindDO = sourceDO.getCustServEventKind();
			resultVO.setCustServEventKindId(custEventKindDO.getKeyword());
			resultVO.setCustServEventNatureId(custEventKindDO.getCustEventNature().getKeyword());
		}
		if(!BlankUtil.isBlank(sourceDO.getEmergencyDegree()))
		{
			resultVO.setEmergencyDegreeId(sourceDO.getEmergencyDegree().getKeyword());
		}
		return resultVO;
	}


	protected boolean isNameExisit(String name)
	{
		return returnVisitRuleDao.findUniqueByName(name) != null;
	}


	public List<ReturnVisitRuleVO> queryAllReturnVisitRule()
	{
		List<ReturnVisitRuleDO> returnVisitRuleDOList = returnVisitRuleDao.findAll();
		sortList(returnVisitRuleDOList);
		return assembleDOList2VOList(returnVisitRuleDOList);
	}


	public Page queryReturnVisitRuleListByPage(SearchVO searchVO)
	{
		searchVO.setSortStr("sequence ASC");
		return queryVOListByPage(searchVO);
	}


	public void setReturnVisitRuleDao(IReturnVisitRuleDao returnVisitRuleDao)
	{
		this.returnVisitRuleDao = returnVisitRuleDao;
	}


	public void setCustEventKindDao(ICustEventKindDao custEventKindDao)
	{
		this.custEventKindDao = custEventKindDao;
	}


	public void setEmergencyDegreeDao(IEmergencyDegreeDao emergencyDegreeDao)
	{
		this.emergencyDegreeDao = emergencyDegreeDao;
	}


	@Override
	protected ReturnVisitRuleDO findObjectDOById(Long id)
	{
		return returnVisitRuleDao.findById(id);
	}


	@SuppressWarnings("unchecked")
	@Override
	protected Page queryDOListByPage(SearchVO searchVO)
	{

		Page page = returnVisitRuleDao.queryByPage(searchVO);
		List<ReturnVisitRuleDO> list = page.getResult();
		sortList(list);
		page.setResult(list);
		return page;
	}

	@Override
	protected int compareDO(ReturnVisitRuleDO o1, ReturnVisitRuleDO o2)
	{
		int s1 = Integer.MAX_VALUE;
		int s2 = Integer.MAX_VALUE;
		if(o1.getSequence() != null)
		{
			s1 = o1.getSequence();
		}
		if(o2.getSequence() != null)
		{
			s2 = o2.getSequence();
		}
		return s1 - s2;
	}
	
	public void setCustEventNatureDao(ICustEventNatureDao custEventNatureDao)
	{
		this.custEventNatureDao = custEventNatureDao;
	}


	@Override
	protected String getNameExisitExceptionInoKey()
	{
		return "custservice:system_name_exist_exception";
	}
}
